package view;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Insets;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.security.acl.Group;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.swing.BorderFactory;
import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.GroupLayout.Alignment;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.border.Border;
import javax.swing.border.TitledBorder;
import javax.swing.ImageIcon;

import utils.ValidateFormTools;
import model.bean.Employee;
import model.dao.EmployeeDAO;

public class LoginView extends JFrame {
	//declaration private variables 
	private JButton btnClose;
	private JButton btnLogin;
	
	private JLabel lbUserIdLogin;
	private JLabel lbPasswordLogin;
	
	private JPanel pnlLogin;
	
	private JPasswordField txtPassword;
	private JTextField txtUserId;
	
	public LoginView() {
		
		initComponent();
		centerWindow();
	}
	
	private void initComponent() {
		
		pnlLogin		=	new JPanel();
		lbUserIdLogin	=	new JLabel();
		lbPasswordLogin	=	new JLabel();
		txtUserId		=	new	JTextField();
		txtPassword		=	new	JPasswordField();
		btnClose		=	new JButton();
		btnLogin		=	new	JButton();
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		setTitle("LOG IN");
		pnlLogin.setBorder(BorderFactory.createTitledBorder(null, "Login", TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_POSITION,	new Font("Calibri", 1, 14)));
		pnlLogin.setPreferredSize(new Dimension(377, 200));
		
		lbUserIdLogin.setText("User Id:");
		lbPasswordLogin.setText("Password");

		btnClose.setIcon(new ImageIcon(getClass().getResource("/images/logout.png")));
		btnClose.setText("Exit");
		btnClose.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				btnCloseActionPerformed(e);
				
			}
		});
		
		btnLogin.setIcon(new ImageIcon(getClass().getResource("../images/key.png")));
		btnLogin.setText("Login");
		btnLogin.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				btnLoginActionPerformed(e);
				
			}
		});
		
		GroupLayout pnlLoginLayout	= new GroupLayout(pnlLogin);
		pnlLogin.setLayout(pnlLoginLayout);
		pnlLoginLayout.setHorizontalGroup(
				pnlLoginLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(pnlLoginLayout.createSequentialGroup()
						.addGroup(pnlLoginLayout.createParallelGroup(Alignment.TRAILING)
								.addGroup(Alignment.LEADING, pnlLoginLayout.createSequentialGroup()
										.addContainerGap()
										.addGroup(pnlLoginLayout.createParallelGroup(Alignment.TRAILING, false)
												.addGroup(Alignment.LEADING, pnlLoginLayout.createSequentialGroup()
														.addComponent(lbPasswordLogin, GroupLayout.PREFERRED_SIZE, 70, GroupLayout.PREFERRED_SIZE)
														.addGap(18, 18, 18)
														.addComponent(txtPassword))
												.addGroup(Alignment.LEADING, pnlLoginLayout.createSequentialGroup()
														.addComponent(lbUserIdLogin, GroupLayout.PREFERRED_SIZE, 70, GroupLayout.PREFERRED_SIZE)
														.addGap(18, 18, 18)
														.addComponent(txtUserId, GroupLayout.PREFERRED_SIZE, 210, GroupLayout.PREFERRED_SIZE))))
								.addGroup(Alignment.LEADING, pnlLoginLayout.createSequentialGroup()
										.addGap(52, 52, 52)
										.addComponent(btnLogin, GroupLayout.PREFERRED_SIZE, 124, GroupLayout.PREFERRED_SIZE)
										.addPreferredGap(ComponentPlacement.RELATED, 55, Short.MAX_VALUE)
										.addComponent(btnClose, GroupLayout.PREFERRED_SIZE, 112, GroupLayout.PREFERRED_SIZE)))
							.addContainerGap(76, GroupLayout.PREFERRED_SIZE))
				);
		
		pnlLoginLayout.setVerticalGroup(
				pnlLoginLayout.createParallelGroup()
				.addGroup(pnlLoginLayout.createSequentialGroup()
						.addContainerGap()
						.addGroup(pnlLoginLayout.createParallelGroup(Alignment.LEADING)
								.addGroup(pnlLoginLayout.createSequentialGroup()
										.addComponent(txtUserId, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
										.addPreferredGap(ComponentPlacement.RELATED)
										.addGroup(pnlLoginLayout.createParallelGroup(Alignment.BASELINE)
												.addComponent(lbPasswordLogin)
												.addComponent(txtPassword, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)))
										.addComponent(lbUserIdLogin))
								.addPreferredGap(ComponentPlacement.RELATED)
								.addGroup(pnlLoginLayout.createParallelGroup(Alignment.BASELINE)
										.addComponent(btnClose)
										.addComponent(btnLogin))
										.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
		);
		
		getContentPane().add(pnlLogin, BorderLayout.CENTER);
		
		pack();
	}
	
	private void centerWindow(){
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		int height = screenSize.height;
		int width  = screenSize.width;
		setBounds((width-400)/2, (height-320)/2, 400, 320);
		setIconImage((new ImageIcon("wire_transfer.png")).getImage());
	}
	
	private void btnCloseActionPerformed(ActionEvent e){
		
		if (JOptionPane.showConfirmDialog(this, "You want to close this app?", "Verify close", JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE) == JOptionPane.OK_OPTION) {
			System.exit(1);
		}
	}
	
	private void btnLoginActionPerformed(ActionEvent e){
		Login();
	}
	
	private void Login(){
		if (validateLogin()) {//xac nhan dang nhap
			//try{
				String rawPass = new String(txtPassword.getPassword());
				Employee emp = EmployeeDAO.isValidUser(txtUserId.getText().trim(), rawPass);
				
				if(emp != null){//neu mot nhan vien duoc tim thay
					MainView vMain = new MainView(emp);//check is admin
					vMain.setVisible(true);
					txtPassword.setText("");
					txtUserId.setText("");
					this.setVisible(false);
				}else{//neu k tim duoc nhan vien
					JOptionPane.showMessageDialog(null, "UserId or Password is not true!", "Login Fail", JOptionPane.ERROR_MESSAGE);
				}
//			}catch(ClassNotFoundException ce){
//				JOptionPane.showMessageDialog(null, "Can't connect to Database", "Login Fail", JOptionPane.ERROR_MESSAGE);
//				Logger.getLogger(LoginView.class.getName()).log(Level.SEVERE, null, ce);
//			}
		}
	}
	
	private boolean validateLogin(){
		StringBuilder msg = new StringBuilder();
		//check userid
		if(!ValidateFormTools.isFilledTextField(txtUserId, true)){
			msg.append("\n- You must use your UserId.");
		}
		
		//check Password not null
		if((new String(txtPassword.getPassword())).compareTo("") == 0){
			msg.append("\n- Password must not null.");
		}
		
		if(msg.toString().compareTo("") == 0){
			return true;
		}else{
			JOptionPane.showMessageDialog(null, "there are a some errors" + msg.toString(), "Error Login", JOptionPane.ERROR_MESSAGE);
			return false;
		}
	}

	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			
			@Override
			public void run() {
				new LoginView().setVisible(true);
				
			}
		});
	}
	
}
